﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DockSample
{
    class GenerateDiagramUtil
    {
        public static void generateInforAndStateDiagram()
        {
            generateInforDiagram();
            generateStateDiagram();
        }

        public static void generateInforDiagram()
        {
            List<DummyModelForm> tmpFormList = new List<DummyModelForm>();
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                if (tmpForm is ObjectModelForm)
                {
                    ObjectModelForm objForm = (ObjectModelForm)tmpForm;

                    foreach (OriginalDiagram tmpDiag in objForm.diagramList)
                    {
                        ObjectDiagram objDiag = (ObjectDiagram)tmpDiag;
                        if (objDiag.AttributeList.Count != 0 && objDiag.AssocInforModelForm == null)
                        {
                            InformationFlowModelForm ifmf = SystemVar.mainForm.createNewInforFlowModelForm("InformationFlowModelForm: Decompose of " + objDiag.Name);
                            ifmf.TopLevel = false;//要不要添加父Form//
                            ifmf.Parent = SystemVar.mainForm;
                            //SystemVar.mainForm.modelFormList.Add(ifmf);//InformationFlowModelForm手动添加，而不是在构造函数添加
                            tmpFormList.Add(ifmf);
                            GenerateDiagramUtil.decomposeMethodToInformationDiagram(objDiag, ifmf);
                        }
                    }
                }
            }
            foreach (DummyModelForm tmpForm in tmpFormList)
            {
                SystemVar.mainForm.modelFormList.Add(tmpForm);
            }
        }

        public static void generateStateDiagram()
        {
            List<DummyModelForm> tmpFormList = new List<DummyModelForm>();
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                if (tmpForm is ObjectModelForm)
                {
                    ObjectModelForm objForm = (ObjectModelForm)tmpForm;

                    foreach (OriginalDiagram tmpDiag in objForm.diagramList)
                    {
                        ObjectDiagram objDiag = (ObjectDiagram)tmpDiag;
                        if (objDiag.MethodList.Count != 0 && objDiag.AssocStateModelForm == null)
                        {
                            StateTransitionModelForm stmf = SystemVar.mainForm.createNewStateModelForm("InformationFlowModelForm: Decompose of " + objDiag.Name);
                            stmf.TopLevel = false;
                            stmf.Parent = SystemVar.mainForm;
                            tmpFormList.Add(stmf);
                            GenerateDiagramUtil.decomposeAttrToStateDiagram(objDiag, stmf);
                        }
                    }
                }
            }
            foreach (DummyModelForm tmpForm in tmpFormList)
            {
                SystemVar.mainForm.modelFormList.Add(tmpForm);
            }
        }

        public static void re_generateInforAndStateDiagram()
        {
            List<DummyModelForm> tmpAddedModelFormList = new List<DummyModelForm>();
            List<DummyModelForm> tmpReMovedModelFormList = new List<DummyModelForm>();

            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                if (tmpForm is ObjectModelForm)
                {
                    ObjectModelForm objForm = (ObjectModelForm)tmpForm;

                    foreach (OriginalDiagram tmpDiag in objForm.diagramList)
                    {
                        ObjectDiagram objDiag = (ObjectDiagram)tmpDiag;
                        if (objDiag.MethodList.Count != 0)
                        {
                            InformationFlowModelForm ifmf = SystemVar.mainForm.createNewInforFlowModelForm("InformationFlowModelForm: Decompose of " + objDiag.Name);
                            ifmf.TopLevel = false;
                            tmpAddedModelFormList.Add(ifmf);
                            GenerateDiagramUtil.decomposeMethodToInformationDiagram(objDiag, ifmf);
                        }

                        if (objDiag.AttributeList.Count != 0)
                        {
                            StateTransitionModelForm stmf = SystemVar.mainForm.createNewStateModelForm("StateTransitionModelForm: Decompose of " + objDiag.Name);
                            stmf.TopLevel = false;
                            tmpAddedModelFormList.Add(stmf);
                            GenerateDiagramUtil.decomposeAttrToStateDiagram(objDiag, stmf);
                        }
                    }
                }
                else
                {
                    tmpReMovedModelFormList.Add(tmpForm);
                }
            }

            foreach (DummyModelForm tmpForm in tmpAddedModelFormList)
            {
                //tmpForm.Parent = SystemVar.mainForm;
                SystemVar.mainForm.modelFormList.Add(tmpForm);
            }

            foreach (DummyModelForm tmpForm in tmpReMovedModelFormList)
            {

                SystemVar.mainForm.modelFormList.Remove(tmpForm);
                tmpForm.Close();
            }
        }

        public static void re_generateInforDiagram()
        {
            List<DummyModelForm> tmpAddedModelFormList = new List<DummyModelForm>();
            List<DummyModelForm> tmpReMovedModelFormList = new List<DummyModelForm>();

            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                if (tmpForm is ObjectModelForm)
                {
                    ObjectModelForm objForm = (ObjectModelForm)tmpForm;

                    foreach (OriginalDiagram tmpDiag in objForm.diagramList)
                    {
                        ObjectDiagram objDiag = (ObjectDiagram)tmpDiag;
                        if (objDiag.MethodList.Count != 0)
                        {
                            InformationFlowModelForm ifmf = SystemVar.mainForm.createNewInforFlowModelForm("InformationFlowModelForm: Decompose of " + objDiag.Name);
                            ifmf.TopLevel = false;
                            ifmf.Parent = SystemVar.mainForm;
                            tmpAddedModelFormList.Add(ifmf);
                            GenerateDiagramUtil.decomposeMethodToInformationDiagram(objDiag, ifmf);
                        }
                    }
                }
                else
                {
                    tmpReMovedModelFormList.Add(tmpForm);
                    //tmpForm.Close();
                }
            }
            //添加InforForm和StateForm到mainForm的Form链表
            foreach (DummyModelForm tmpForm in tmpAddedModelFormList)
            {
                tmpForm.Parent = SystemVar.mainForm;
                SystemVar.mainForm.modelFormList.Add(tmpForm);
            }

            foreach (DummyModelForm tmpForm in tmpReMovedModelFormList)
            {
                
                SystemVar.mainForm.modelFormList.Remove(tmpForm);
                tmpForm.Close();
            }
        }

        public static void re_generateStateDiagram()
        {
            List<DummyModelForm> tmpModelFormList = new List<DummyModelForm>();
            List<DummyModelForm> tmpReMovedModelFormList = new List<DummyModelForm>();

            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                if (tmpForm is ObjectModelForm)
                {
                    ObjectModelForm objForm = (ObjectModelForm)tmpForm;

                    foreach (OriginalDiagram tmpDiag in objForm.diagramList)
                    {
                        ObjectDiagram objDiag = (ObjectDiagram)tmpDiag;
                        if (objDiag.AttributeList.Count != 0)
                        {
                            //产生对象时会改变SystemVar.mainForm.modelFormList   会不会引发问题？？？
                            StateTransitionModelForm stmf = SystemVar.mainForm.createNewStateModelForm("StateTransitionModelForm: Decompose of " + objDiag.Name);
                            stmf.TopLevel = false;
                            stmf.Parent = SystemVar.mainForm;
                            tmpModelFormList.Add(stmf);
                            GenerateDiagramUtil.decomposeAttrToStateDiagram(objDiag, stmf);
                        }
                    }
                }
                else
                {
                    tmpReMovedModelFormList.Add(tmpForm);
                }
            }
            
            foreach (DummyModelForm tmpForm in tmpModelFormList)
            {
                tmpForm.Parent = SystemVar.mainForm;
                SystemVar.mainForm.modelFormList.Add(tmpForm);
            }

            foreach (DummyModelForm tmpForm in tmpReMovedModelFormList)
            {
                SystemVar.mainForm.modelFormList.Remove(tmpForm);
                tmpForm.Close();
            }
        }

        public static void decomposeMethodToInformationDiagram(ObjectDiagram objDiag, InformationFlowModelForm infoForm)
        {
            List<Method> methods = objDiag.MethodList;
            for (int i = 0; i < methods.Count; i++)
            {
                Method m = methods[i];


                int j = i / 4;
                int k = i % 4;
                int x = 100 + k * 180;
                int y = 80 + 120 * j;
                Point p = new Point(x, y);
                InformationDiagram infoD = null;
                if (!m.isComplex)
                {
                    infoD = new ProcessDiagram(p);

                }
                else
                {
                    infoD = new InformationSourceDiagram(p);

                }

                infoD.Name = m.MethodName;
                m.assoInfo = infoD;
                m.assoInfoId = infoD.id;
                infoD.IsSelected = false;
                infoForm.diagramList.Add(infoD);

                infoForm.Invalidate();
            }
            //wjw++
            objDiag.AssocInforModelForm = infoForm;
            objDiag.assocInforModelFormId = infoForm.id;
        }

        public static void decomposeAttrToStateDiagram(ObjectDiagram objectDiagram, StateTransitionModelForm stateForm)
        {
            List<Attribute> attrs = objectDiagram.AttributeList;
            for (int i = 0; i < attrs.Count; i++)
            {
                Attribute attr = attrs[i];


                int j = i / 4;
                int k = i % 4;
                int x = 100 + k * 180;
                int y = 80 + 120 * j;
                Point p = new Point(x, y);
                StateDiagram sd = null;
                if (!attr.isComplex)
                {
                    sd = new PrimitiveStateDiagram(p);

                }
                else
                {
                    sd = new HighStateDiagram(p);

                }

                sd.Name = attr.AttributeName;
                attr.assoState = sd;
                attr.assoStateId = sd.id;
                sd.IsSelected = false;
                stateForm.diagramList.Add(sd);

                stateForm.Invalidate();
            }
            //wjw++
            objectDiagram.AssocStateModelForm = stateForm;
            objectDiagram.assocStateModelFormId = stateForm.id;
        }
    }
}
